//
//  PermissionManager.m
//  plugin_image
//
//  Created by liting on 2025/8/22.
//

#import "PermissionManager.h"

@implementation PermissionManager

+ (instancetype)sharedManager {
    static PermissionManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[PermissionManager alloc] init];
    });
    return sharedInstance;
}


- (void)checkPermissionForType:(PermissionType)type completion:(PermissionResultBlock)completion {
    switch (type) {
        case PermissionTypeCamera:
            [self checkCameraPermissionWithCompletion:completion];
            break;
        case PermissionTypePhotoLibrary:
            [self checkPhotoLibraryPermissionWithCompletion:completion];
            break;
    }
}

- (void)requestPermissionForType:(PermissionType)type completion:(PermissionResultBlock)completion {
    switch (type) {
        case PermissionTypeCamera:
            [self requestCameraPermissionWithCompletion:completion];
            break;
        case PermissionTypePhotoLibrary:
            [self requestPhotoLibraryPermissionWithCompletion:completion];
            break;
    }
}

#pragma mark - Camera Permission

- (void)checkCameraPermissionWithCompletion:(PermissionResultBlock)completion {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    [self handleCameraStatus:status completion:completion];
}

- (void)requestCameraPermissionWithCompletion:(PermissionResultBlock)completion {
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(granted, granted ? @"相机授权成功" : @"相机授权被拒绝");
            }
        });
    }];
}

- (void)handleCameraStatus:(AVAuthorizationStatus)status completion:(PermissionResultBlock)completion {
    switch (status) {
        case AVAuthorizationStatusAuthorized:
            if (completion) completion(YES, @"相机已授权");
            break;
        case AVAuthorizationStatusNotDetermined:
            if (completion) completion(NO, @"相机未请求授权");
            break;
        case AVAuthorizationStatusDenied:
            if (completion) completion(NO, @"相机访问被拒绝");
            break;
        case AVAuthorizationStatusRestricted:
            if (completion) completion(NO, @"相机访问受限");
            break;
    }
}

#pragma mark - Photo Library Permission

- (void)checkPhotoLibraryPermissionWithCompletion:(PermissionResultBlock)completion {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    [self handlePhotoLibraryStatus:status completion:completion];
}

- (void)requestPhotoLibraryPermissionWithCompletion:(PermissionResultBlock)completion {
    if (@available(iOS 14, *)) {
        [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self handlePhotoLibraryStatus:status completion:completion];
            });
        }];
    } else {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self handlePhotoLibraryStatus:status completion:completion];
            });
        }];
    }
}

- (void)handlePhotoLibraryStatus:(PHAuthorizationStatus)status completion:(PermissionResultBlock)completion {
    switch (status) {
        case PHAuthorizationStatusAuthorized:
        case PHAuthorizationStatusLimited:
            if (completion) completion(YES, @"相册已授权");
            break;
        case PHAuthorizationStatusNotDetermined:
            if (completion) completion(NO, @"相册未请求授权");
            break;
        case PHAuthorizationStatusDenied:
            if (completion) completion(NO, @"相册访问被拒绝");
            break;
        case PHAuthorizationStatusRestricted:
            if (completion) completion(NO, @"相册访问受限");
            break;
    }
}

@end
